#!/bin/bash

TMP=`tempfile`
TMP2=`tempfile`
sox "$1" -c1 -t raw -f -4 $TMP

sox_exit_value=$?
if [ $sox_exit_value != 0 ] ; then
  echo "E: sox failed with ${sox_exit_value}"
  exit ${sox_exit_value}
fi

gnuplot << EOF
set terminal png transparent size $3,$4
set output "$TMP2"
set size 2,1
set origin -1.01,0

unset xlabel
unset ylabel

unset xtics
unset ytics

unset border

set offsets 0,0,0,0

set bmargin 0
set rmargin 0
set rmargin 0
set tmargin 0

set style line 1 lt 2 lc rgb "blue" lw 1

plot "$TMP" binary format="%float" using 1 ls 1 with lines notitle
EOF

gnuplot_exit_value=$?
if [ $gnuplot_exit_value != 0 ] ; then
  echo "E: gnuplot failed with ${gnuplot_exit_value}"
  exit ${gnuplot_exit_value}
else
  cp "$TMP2" "$2"
fi


rm $TMP
